package com.hsyt.pms.org.dubbo.impl;


import com.hsyt.pms.api.entity.org.Org;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractSearcher {

    protected abstract SearchRequestBuilder getSearchRequestBuilder(TransportClient esClient, Org org);

    protected abstract HighlightBuilder getHighlightBuilder();

    protected List<SortBuilder> getSortBuilders(){
        List<SortBuilder> sorts = new ArrayList<>();
        sorts.add(SortBuilders.scoreSort().order(SortOrder.DESC));
        return sorts;
    }

    protected BulkProcessor genBulkProcessor(TransportClient esClient) {
        return BulkProcessor.builder(
                esClient,
                new BulkProcessor.Listener() {
                    @Override
                    public void beforeBulk(long l, BulkRequest bulkRequest) {

                    }

                    @Override
                    public void afterBulk(long l, BulkRequest bulkRequest,
                                          BulkResponse bulkResponse) {

                    }

                    @Override
                    public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {

                    }
                })
                .setBulkActions(300)
                .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))
                .setConcurrentRequests(1)
                .setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
                .build();
    }

    protected static BulkProcessor genBulkProcessorTest(TransportClient esClient) {
        return BulkProcessor.builder(
                esClient,
                new BulkProcessor.Listener() {
                    @Override
                    public void beforeBulk(long l, BulkRequest bulkRequest) {

                    }

                    @Override
                    public void afterBulk(long l, BulkRequest bulkRequest,
                                          BulkResponse bulkResponse) {

                    }

                    @Override
                    public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {

                    }
                })
                .setBulkActions(300)
                .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))
                .setConcurrentRequests(1)
                .setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
                .build();
    }
}
